package cn.ekuma.epos.order;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;

import com.openbravo.bean.Location;
import com.openbravo.bean.erp.Order;
import com.openbravo.beans.JNumberDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.qkorder.ordermanager.OrderManager;
import cn.ekuma.epos.qkorder.ordermanager.PageChangeEvent;
import cn.ekuma.epos.qkorder.ordermanager.PageChangeListener;
import java.util.List;

public class JOrderView extends JPanel {

	private JButton jFirstPage;
	private JButton jBeforePage;
	private JButton jCurrPageIndex;
	private JButton jNextPage;
	private JButton jLastPage;
	private JPanel panel;
	JOrderDetail orderDetail;
	protected I_DataLogicERP dlSales;
	protected I_DataLogicSystem dlSystem;
	JOrderLines m_Orderlines;
	protected Order m_oOrder;
	OrderManager orderManager;
	/**
	 * Create the panel.
	 */
	public JOrderView(List<Location> locations,I_DataLogicSystem dlSystem) {
		initComponents();
		orderDetail=new JOrderDetail();
		orderDetail.setLocations(locations);
		add(orderDetail, BorderLayout.NORTH);
		orderManager=new OrderManager(dlSystem);
        orderManager.addPageChangeListener(new PageChangeListener(){
                @Override
                public void pageChanged(PageChangeEvent event) {
                    refreshOrder();
                }
        });
	}
	
	protected void refreshOrder() {
		if (m_oOrder == null) {
            if(m_Orderlines!=null)
                m_Orderlines.clearOrderLines();
            jCurrPageIndex.setText(null);
        } else { 
            jCurrPageIndex.setText(String.valueOf(orderManager.getCurrentPage().getPageIndex()));
            // Limpiamos todas las filas y anadimos las del order actual
            m_Orderlines.clearOrderLines();
            for (int i = 0; i < orderManager.getLinesCount(); i++) {
                m_Orderlines.addOrderLine(orderManager.getLine(i));
            }
        }
	}
	
	private void initComponents() {
        jLastPage = new javax.swing.JButton();

        jLastPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2rightarrow.png"))); // NOI18N
        jLastPage.setFocusable(false);
        jLastPage.setPreferredSize(new java.awt.Dimension(35, 25));
        jLastPage.setRequestFocusEnabled(false);
        jLastPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLastPageActionPerformed(evt);
            }
        });
		setLayout(new BorderLayout(0, 0));
		
		panel = new JPanel();
		add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		jFirstPage = new javax.swing.JButton();
		jBeforePage = new javax.swing.JButton();
		jCurrPageIndex = new javax.swing.JButton();
		jNextPage = new javax.swing.JButton();
		
		JPanel jPanel3 = new JPanel();
		panel.add(jPanel3, BorderLayout.NORTH);
		jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.X_AXIS));
		
		jFirstPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2leftarrow.png"))); // NOI18N
		jFirstPage.setFocusable(false);
		jFirstPage.setPreferredSize(new java.awt.Dimension(35, 25));
		jFirstPage.setRequestFocusEnabled(false);
		jFirstPage.addActionListener(new java.awt.event.ActionListener() {
		    public void actionPerformed(java.awt.event.ActionEvent evt) {
		        jFirstPageActionPerformed(evt);
		    }
		});
		jPanel3.add(jFirstPage);
		
		        jBeforePage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1leftarrow.png"))); // NOI18N
		        jBeforePage.setFocusable(false);
		        jBeforePage.setPreferredSize(new java.awt.Dimension(35, 25));
		        jBeforePage.addActionListener(new java.awt.event.ActionListener() {
		            public void actionPerformed(java.awt.event.ActionEvent evt) {
		                jBeforePageActionPerformed(evt);
		            }
		        });
		        jPanel3.add(jBeforePage);
		        
		                jCurrPageIndex.setBackground(new java.awt.Color(255, 255, 255));
		                jCurrPageIndex.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
		                jCurrPageIndex.setFocusPainted(false);
		                jCurrPageIndex.setFocusable(false);
		                jCurrPageIndex.setMargin(new java.awt.Insets(0, 0, 0, 0));
		                jCurrPageIndex.setMaximumSize(new java.awt.Dimension(50, 25));
		                jCurrPageIndex.setMinimumSize(new java.awt.Dimension(50, 25));
		                jCurrPageIndex.setPreferredSize(new java.awt.Dimension(40, 25));
		                jCurrPageIndex.setRequestFocusEnabled(false);
		                jCurrPageIndex.setVerifyInputWhenFocusTarget(false);
		                jCurrPageIndex.addActionListener(new java.awt.event.ActionListener() {
		                    public void actionPerformed(java.awt.event.ActionEvent evt) {
		                        jCurrPageIndexActionPerformed(evt);
		                    }
		                });
		                jPanel3.add(jCurrPageIndex);
		                
		                        jNextPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png"))); // NOI18N
		                        jNextPage.setFocusable(false);
		                        jNextPage.setPreferredSize(new java.awt.Dimension(35, 25));
		                        jNextPage.setRequestFocusEnabled(false);
		                        jNextPage.addActionListener(new java.awt.event.ActionListener() {
		                            public void actionPerformed(java.awt.event.ActionEvent evt) {
		                                jNextPageActionPerformed(evt);
		                            }
		                        });
		                        jPanel3.add(jNextPage);
	}
	
	 private void jFirstPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFirstPageActionPerformed
	        orderManager.pageFirst();
	    }//GEN-LAST:event_jFirstPageActionPerformed

	    private void jBeforePageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBeforePageActionPerformed
	         orderManager.pageUp();
	    }//GEN-LAST:event_jBeforePageActionPerformed

	    private void jNextPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNextPageActionPerformed
	        orderManager.pageDown();
	    }//GEN-LAST:event_jNextPageActionPerformed

	    private void jLastPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLastPageActionPerformed
	         orderManager.pageEnd();
	    }//GEN-LAST:event_jLastPageActionPerformed
	    
	    private void jCurrPageIndexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCurrPageIndexActionPerformed
            Double advCode = JNumberDialog .showEditNumber(this,
               AppLocal.getIntString("Label.inputPageNumber"),
               AppLocal.getIntString("Label.inputPageNumber"));
           if(advCode!=null){
                int pageIndex=advCode.intValue();
                orderManager.selectPage(pageIndex);
           }
   }//GEN-LAST:event_jCurrPageIndexActionPerformed

    private void buildOrderLinePanel(){
        if(m_Orderlines!=null)
        	panel.remove(m_Orderlines);
        String lineStyle="Order.Line";
        String lineColumnXML=dlSystem.getResourceAsXML(lineStyle+"."+m_oOrder.getDefineType().getName());
        if(lineColumnXML==null||lineColumnXML.isEmpty())
            lineColumnXML=dlSystem.getResourceAsXML(lineStyle);
        m_Orderlines = new JOrderLines(lineColumnXML);
        panel.add(m_Orderlines, java.awt.BorderLayout.CENTER);
    }
    

    public void setActiveOrder(Order oOrder) {
        m_oOrder = oOrder;
        if (m_oOrder != null) {
            buildOrderLinePanel();
            try {
                orderManager.setOrder(oOrder, false);
            } catch (BasicException ex) {
                new MessageInf(ex).show(this);
            }
        }
        refreshOrder();
    }
    
    public Order getActiveOrder(){
    	return  m_oOrder;
    }
}
